/**
 *====================================================
 * 文件名称: BulkstockCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年10月23日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.action.v1.dto;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.BooleanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.bulkstock.entity.lexicon.MeasurementEnum;
import com.yuexunit.finance.bulkstock.service.BulkstockInfoService;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.CollectionUtils;
import com.yuexunit.finance.member.entity.LogisticsCompanyInfoEntity;
import com.yuexunit.finance.member.service.LogisticsCompanyInfoService;
import com.yuexunit.finance.project.action.v1.FinanceApplicationActionApi;
import com.yuexunit.finance.project.action.v1.model.BulkstockInventory;
import com.yuexunit.finance.project.action.v1.model.DetainedInventory;
import com.yuexunit.finance.project.action.v1.model.FinanceBulkstock;
import com.yuexunit.finance.project.action.v1.model.FinanceBulkstockPublic;
import com.yuexunit.finance.project.action.v1.model.input.FinanceApplication;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalInventoryDetail;
import com.yuexunit.finance.project.action.v1.model.output.ContainerBulkstock;
import com.yuexunit.finance.project.action.v1.model.output.ContainerBulkstockHistory;
import com.yuexunit.finance.project.action.v1.model.output.ContainerInventory;
import com.yuexunit.finance.project.action.v1.model.output.ContainerInventoryHistory;
import com.yuexunit.finance.project.action.v1.model.output.HandleSuitcaseResult;
import com.yuexunit.finance.project.action.v1.model.output.PickupBulkstock;
import com.yuexunit.finance.project.entity.AbnormalInventoryEntity;
import com.yuexunit.finance.project.entity.ContainerBulkstockEntity;
import com.yuexunit.finance.project.entity.ContainerBulkstockHistoryEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryHistoryEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryRelieveEntity;
import com.yuexunit.finance.project.entity.ContainerInventorySignEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryTallyEntity;
import com.yuexunit.finance.project.entity.DeliveryNoticeEntity;
import com.yuexunit.finance.project.entity.DetainedInventoryEntity;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockColdStorageEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockDangerousEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.FinanceContractEntity;
import com.yuexunit.finance.project.entity.LoanNoticeEntity;
import com.yuexunit.finance.project.entity.PickupBulkstockEntity;
import com.yuexunit.finance.project.entity.RecieveNoticeEntity;
import com.yuexunit.finance.project.entity.RecieveNoticeInventoryEntity;
import com.yuexunit.finance.project.entity.lexicon.BulkstockSourceEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.finance.project.service.ContainerBulkstockHistoryService;
import com.yuexunit.finance.project.service.ContainerBulkstockService;
import com.yuexunit.finance.project.service.ContainerInventoryRelieveService;
import com.yuexunit.finance.project.service.ContainerInventoryService;
import com.yuexunit.finance.project.service.ContainerInventorySignService;
import com.yuexunit.finance.project.service.ContainerInventoryTallyService;
import com.yuexunit.finance.project.service.DeliveryNoticeService;
import com.yuexunit.finance.project.service.FinanceApplicationService;
import com.yuexunit.finance.project.service.FinanceBulkstockColdStorageService;
import com.yuexunit.finance.project.service.FinanceBulkstockDangerousService;
import com.yuexunit.finance.project.service.FinanceBulkstockService;
import com.yuexunit.finance.project.service.FinanceContractService;
import com.yuexunit.finance.project.service.LoanNoticeService;
import com.yuexunit.finance.project.service.RecieveNoticeService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: BulkstockCastor
 * @Description: 融资货物信息/通知发货货物信息/货箱及货物信息/提货货物信息&提货货箱信息
 * @author chenxy
 * @date 2015年10月23日 上午9:56:58
 */
public class BulkstockCastor {

	private static ContainerInventoryService containerInventoryService = ComponentFactory.loadClass(ContainerInventoryService.class);

	private static ContainerInventoryTallyService containerInventoryTallyService = ComponentFactory.loadClass(ContainerInventoryTallyService.class);

	private static ContainerInventorySignService containerInventorySignService = ComponentFactory.loadClass(ContainerInventorySignService.class);

	private static ContainerInventoryRelieveService containerInventoryRelieveService = ComponentFactory.loadClass(ContainerInventoryRelieveService.class);

	private static ContainerBulkstockService containerBulkstockService = ComponentFactory.loadClass(ContainerBulkstockService.class);

	private static ContainerBulkstockHistoryService containerBulkstockHistoryService = ComponentFactory.loadClass(ContainerBulkstockHistoryService.class);

	private static BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);

	private static FinanceBulkstockService financeBulkstockService = ComponentFactory.loadClass(FinanceBulkstockService.class);

	// ===================================================================融资货物信息================================================================================================//
	/** 融资申请 */
	public static List<FinanceBulkstockEntity> fromFinanceApplication(FinanceApplication info, Long financeId) {
		BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);
		List<FinanceBulkstockEntity> entities = new ArrayList<>();
		for (int i = 0; i < info.getBulkstockInfoIds().length; i++) {
			FinanceBulkstockEntity entity = new FinanceBulkstockEntity();
			entity.setBulkstockSource(BulkstockSourceEnum.FINANCE);
			entity.setBulkstockInfoId(info.getBulkstockInfoIds()[i]);
			BulkstockInfoEntity bulkstockInfo = bulkstockInfoService.inquireBulkstockInfo(entity.getBulkstockInfoId());
			entity.setBulkstockCategoryId(bulkstockInfo.getBulkstockCategoryId());
			entity.setBulkstockNumber(bulkstockInfo.getBulkstockNumber());
			entity.setSpecification(bulkstockInfo.getSpecificationName());
			entity.setBulkstockName(bulkstockInfo.getBulkstockName());

			entity.setFinanceId(financeId);
			if (ProductTypeEnum.MANIFEST_PLEDGE.ordinal() == info.getProductType() || ProductTypeEnum.PROXY_SALE.ordinal() == info.getProductType() || ProductTypeEnum.PURCHASING_STORAGE.ordinal() == info.getProductType()) {
				if (bulkstockInfo.getGrossWeight() == null) {
					throw new BusinessException("请联系工作人员补全商品【" + bulkstockInfo.getBulkstockName() + "(" + bulkstockInfo.getSpecificationName() + ")】的信息！");
				} else {
					entity.setWeight(((info.getQuantitys()[i]).multiply(bulkstockInfo.getGrossWeight())).divide(new BigDecimal(1000)));
				}
				
				if (bulkstockInfo.getVolume() == null) {
					entity.setVolume(BigDecimal.ZERO);
				} else {
					entity.setVolume((info.getQuantitys()[i]).multiply(bulkstockInfo.getVolume()));
				}
			} else {
				if (null != info.getWeights()[i]) {
					entity.setWeight(info.getWeights()[i]);
				} else {
					entity.setWeight(BigDecimal.ZERO);
				}
				
				if (null != info.getVolumes()[i]) {
					entity.setVolume(info.getVolumes()[i]);
				} else {
					entity.setVolume(BigDecimal.ZERO);
				}
			}
			
			entity.setUnit(info.getUnits()[i]);
			entity.setQuantity(info.getQuantitys()[i]);
			entity.setMeasurement(bulkstockInfo.getMeasurement());
			entity.setPackingMethod(info.getPackingMethod()[i]);
			entity.setBulkstockGrade(info.getBulkstockGrade()[i]);
	 		
			if (MeasurementEnum.VOLUME == entity.getMeasurement()) { // 体积计价单位
				entity.setValuationQuantity(entity.getVolume());
			} else { // 重量计价单位
				entity.setValuationQuantity(entity.getWeight());
			}
			if (null != info.getOriginPlaces()) {
				entity.setOriginPlace(info.getOriginPlaces()[i]);
			}
			
			if (null != info.getManufacturers()) {
				entity.setManufacturer(info.getManufacturers()[i]);
			}
			
			if (null != info.getPublicity()) {
				entity.setPublicity(info.getPublicity()[i]);
			}
			
			entity.setCargoMark(CollectionUtils.getArrayIndexValue(info.getCargoMark(), i));
			if (info.getColdStorage() == null) {
				entity.setColdStorage(false);
			} else {
				entity.setColdStorage(BooleanUtils.toBoolean(CollectionUtils.getArrayIndexValue(info.getColdStorage(), i)));
			}
			if (info.getDangerous() == null) {
				entity.setDangerous(false);
			} else {
				entity.setDangerous(BooleanUtils.toBoolean(CollectionUtils.getArrayIndexValue(info.getDangerous(), i)));
			}
			if (CollectionUtils.getArrayIndexValue(info.getBulkstockPrice(), i) != null) {
				entity.setBulkstockPrice(CollectionUtils.getArrayIndexValue(info.getBulkstockPrice(), i));
			}
			entity.setEvaluatePrice(BigDecimal.ZERO);
			if (entity.getBulkstockPrice() == null) {
				throw new BusinessException("货物估值单价为空");
			}
			entity.setValuationDate(DateUtil.todayInt());
			entity.setCargoMark(CollectionUtils.getArrayIndexValue(info.getCargoMark(), i));
			if (entity.getColdStorage()) { // 冷藏保存
				FinanceBulkstockColdStorageEntity bulkstockColdStorage = new FinanceBulkstockColdStorageEntity();
				bulkstockColdStorage.setFinanceId(financeId);
				bulkstockColdStorage.setCoolLetter(CollectionUtils.getArrayIndexValue(info.getCoolLetter(), i));
				bulkstockColdStorage.setCoolRemark(CollectionUtils.getArrayIndexValue(info.getCoolRemark(), i));
				bulkstockColdStorage.setPlug(BooleanUtils.toBoolean(CollectionUtils.getArrayIndexValue(info.getPlug(), i)));
				bulkstockColdStorage.setPlugType(CollectionUtils.getArrayIndexValue(info.getPlugType(), i));
				bulkstockColdStorage.setPrecool(BooleanUtils.toBoolean(CollectionUtils.getArrayIndexValue(info.getPrecool(), i)));
				bulkstockColdStorage.setPtiOk(BooleanUtils.toBoolean(CollectionUtils.getArrayIndexValue(info.getPtiOk(), i)));
				bulkstockColdStorage.setSetTemperature(CollectionUtils.getArrayIndexValue(info.getSetTemperature(), i));
				bulkstockColdStorage.setTempMax(CollectionUtils.getArrayIndexValue(info.getTempMax(), i));
				bulkstockColdStorage.setTempMin(CollectionUtils.getArrayIndexValue(info.getTempMin(), i));
				bulkstockColdStorage.setTempUnit(CollectionUtils.getArrayIndexValue(info.getTempUnit(), i));
				bulkstockColdStorage.setTempUpDown(CollectionUtils.getArrayIndexValue(info.getTempUpDown(), i));
				bulkstockColdStorage.setVent(CollectionUtils.getArrayIndexValue(info.getVent(), i));
				entity.setBulkstockColdStorage(bulkstockColdStorage);
			}
			if (entity.getDangerous()) { // 危险品
				FinanceBulkstockDangerousEntity bulkstockDangerous = new FinanceBulkstockDangerousEntity();
				bulkstockDangerous.setFinanceId(financeId);
				bulkstockDangerous.setDangerousRemark(CollectionUtils.getArrayIndexValue(info.getDangerousRemark(), i));
				bulkstockDangerous.setDeclaration(CollectionUtils.getArrayIndexValue(info.getDeclaration(), i));
				bulkstockDangerous.setImdg(CollectionUtils.getArrayIndexValue(info.getImdg(), i));
				bulkstockDangerous.setInstructionManual(CollectionUtils.getArrayIndexValue(info.getInstructionManual(), i));
				bulkstockDangerous.setUnNum(CollectionUtils.getArrayIndexValue(info.getUnNum(), i));
				entity.setBulkstockDangerous(bulkstockDangerous);
			}
			entities.add(entity);
		}
		return entities;
	}

	/** 融资货物信息 */
	public static FinanceBulkstock toFinanceBulkstock(FinanceBulkstockEntity entity) {
		FinanceBulkstock bulkstock = new FinanceBulkstock();
		BeanUtils.copyProperties(entity, bulkstock);
		if (entity.getVolume() != null) {
			bulkstock.setVolume(entity.getVolume().toString());
		}
		bulkstock.setMeasurement(entity.getMeasurement().ordinal());
		bulkstock.setPublicity(BooleanUtils.toIntegerObject(entity.getPublicity()));
		bulkstock.setValuationDate(DateUtil.formatDate(entity.getValuationDate()));
		bulkstock.setFinanceValuation(bulkstock.getFinancePrice().multiply(bulkstock.getValuationQuantity()).setScale(2, BigDecimal.ROUND_DOWN));
		bulkstock.setEvaluateValuation(bulkstock.getEvaluatePrice().multiply(bulkstock.getValuationQuantity()).setScale(2, BigDecimal.ROUND_HALF_UP));
		bulkstock.setBulkstockValuation(bulkstock.getBulkstockPrice().multiply(bulkstock.getValuationQuantity()));
		bulkstock.setHighestPrice(entity.getHighestPrice());
		bulkstock.setLowestPrice(entity.getLowestPrice());

		BulkstockInfoEntity bulkstockInfo = bulkstockInfoService.inquireBulkstockInfo(entity.getBulkstockInfoId());
		bulkstock.setConversionFactor(bulkstockInfo.getConversionFactor());

		if (entity.getColdStorage()) {
			FinanceBulkstockColdStorageService financeBulkstockColdStorageService = ComponentFactory.loadClass(FinanceBulkstockColdStorageService.class);
			FinanceBulkstockColdStorageEntity coldStorage = financeBulkstockColdStorageService.inquireFinanceBulkstockColdStorage(entity.getBulkstockId());
			BeanUtils.copyProperties(coldStorage, bulkstock);
		}
		if (entity.getDangerous()) {
			FinanceBulkstockDangerousService financeBulkstockDangerousService = ComponentFactory.loadClass(FinanceBulkstockDangerousService.class);
			FinanceBulkstockDangerousEntity dangerous = financeBulkstockDangerousService.inquireFinanceBulkstockDangerous(entity.getBulkstockId());
			BeanUtils.copyProperties(dangerous, bulkstock);
		}
		return bulkstock;
	}

	/** 公示货物信息 */
	public static FinanceBulkstockPublic toFinanceBulkstockPublic(FinanceBulkstockEntity entity) {
		FinanceBulkstockPublic bulkstock = new FinanceBulkstockPublic();
		BeanUtils.copyProperties(entity, bulkstock);

		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		bulkstock.setDeliverAddress(finance.getDeliverAddress());

		return bulkstock;
	}

	// ===================================================================通知发货货物信息================================================================================================//
	public static List<RecieveNoticeInventoryEntity> fromRecieveNoticeDetail(Long recieveNoticeId, Long financeId, Long[] bulkstockIds, BigDecimal[] quantities, String[] caseSize, Integer[] caseQuantities) {
		List<RecieveNoticeInventoryEntity> recieveNoticeDetailEntityList = new ArrayList<RecieveNoticeInventoryEntity>();
		for (int i = 0; i < bulkstockIds.length; i++) {
			FinanceBulkstockEntity fbe = financeBulkstockService.inquireFinanceBulkstock(bulkstockIds[i]);
			RecieveNoticeInventoryEntity recieveInventory = new RecieveNoticeInventoryEntity();
			BeanUtils.copyProperties(fbe, recieveInventory);
			recieveInventory.setRecieveNoticeId(recieveNoticeId);
			recieveInventory.setQuantity(quantities[i]);
			if (fbe.getVolume() != null) {
				BigDecimal volume = recieveInventory.getQuantity().divide(fbe.getQuantity(), 2).multiply(fbe.getVolume());
				recieveInventory.setVolume(volume);
			} else {
				recieveInventory.setVolume(BigDecimal.ZERO);
			}
			BigDecimal weight = recieveInventory.getQuantity().divide(fbe.getQuantity(), 2).multiply(fbe.getWeight());
			recieveInventory.setWeight(weight);
			recieveNoticeDetailEntityList.add(recieveInventory);
		}
		return recieveNoticeDetailEntityList;
	}

	// ===================================================================货箱及货物信息================================================================================================//
	/** 融资货箱信息 */
	public static ContainerInventory toContainerInventory(ContainerInventoryEntity cie, Boolean inventoryState) {
		Logger logger = LoggerFactory.getLogger(BulkstockCastor.class);
		FinanceApplicationActionApi financeApplicationActionApi = ComponentFactory.loadClass(FinanceApplicationActionApi.class);
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(cie.getFinanceId());
		FinanceContractService financeContractService = ComponentFactory.loadClass(FinanceContractService.class);
		RecieveNoticeService recieveNoticeService = ComponentFactory.loadClass(RecieveNoticeService.class);
		LoanNoticeService loanNoticeService = ComponentFactory.loadClass(LoanNoticeService.class);
		DeliveryNoticeService deliveryNoticeService = ComponentFactory.loadClass(DeliveryNoticeService.class);
		DeliveryNoticeEntity deliveryNotice = deliveryNoticeService.inquireDeliveryNotice(cie.getDeliveryNoticeId());
		LoanNoticeEntity loanNotice = loanNoticeService.inquireLoanNoticeEntity(cie.getLoanNoticeId());
		FinanceContractEntity contract = financeContractService.inquireFinanceIdContract(cie.getFinanceId(), null);
		//物流公司信息
		LogisticsCompanyInfoService logisticsCompanyInfoService = ComponentFactory.loadClass(LogisticsCompanyInfoService.class);
		LogisticsCompanyInfoEntity logisticsCompanyInfo = logisticsCompanyInfoService.inquireLogisticsCompanyInfo(cie.getLogisticsCompanyInfoId());
		ContainerInventory ci = new ContainerInventory();
		BeanUtils.copyProperties(cie, ci);
		ci.setAbnormal(BooleanUtils.toIntegerObject(cie.getAbnormal()));
		ci.setAbnormalState(cie.getAbnormalState().ordinal());
		ci.setAbnormalType(cie.getAbnormalType().ordinal());
		ci.setLatestAbnormalResult(cie.getLatestAbnormalResult().ordinal());
		ci.setTallyingState(BooleanUtils.toIntegerObject(cie.getTallyingState()));
//		ci.setPickupModel(cie.getPickupModel().ordinal());
		ci.setProductType(finance.getProductType().ordinal());
		ci.setSignState(BooleanUtils.toIntegerObject(cie.getSignState()));
		ci.setSupervisionState(cie.getSupervisionState().ordinal());
		ci.setDetainedState(BooleanUtils.toIntegerObject(cie.getDetainedState()));
		ci.setRedeemState(BooleanUtils.toIntegerObject(cie.getRedeemState()));
		ci.setBadassetsState(cie.getBadassetsState().ordinal());
		ci.setEnterpriseName(cie.getEnterpriseName());
		ci.setHarborState(cie.getHarborState() == Boolean.TRUE ? 1 : 0);
		ci.setFinanceContractSerialNumber(contract == null ? "" : contract.getFinanceContractSerialNumber());
		if(logisticsCompanyInfo != null) {
			ci.setLogisticsCompanyName1(logisticsCompanyInfo.getLogisticsCompanyName());
		}
		if (Boolean.TRUE == inventoryState) {
			HandleSuitcaseResult hsr = financeApplicationActionApi.containerTaken(cie.getWaybillNumber(), cie.getCaseNumber());
			logger.info("HandleSuitcaseResult:" + hsr);
			if (hsr != null && hsr.getData() != null && hsr.getData().size() > 0 && hsr.getData().get(0).getAttrs() != null) {
				if (hsr.getData().get(0).getAttrs().getCffinancelock() == 1) {
					ci.setContainerTaken(Boolean.TRUE);
				} else {
					ci.setContainerTaken(Boolean.FALSE);
				}
			}
		}
		if (loanNotice != null) {
			ci.setLoanDate(DateUtil.formatDate(loanNotice.getLoanDate()));
		}
		if (deliveryNotice != null) {
			
		}
		ContainerInventoryTallyEntity cite = containerInventoryTallyService.inquireContainerInventoryTally(cie.getInventoryId());
		if (cite != null) {
			BeanUtils.copyProperties(cite, ci);
			if (cite.getTallyExAbnormalState() != null) {
				ci.setTallyExAbnormalState(cite.getTallyExAbnormalState().ordinal());
			}
		}
		
		ContainerInventorySignEntity cise = containerInventorySignService.inquireContainerInventorySign(cie.getInventoryId());
		if (cise != null) {
			BeanUtils.copyProperties(cise, ci);
			if (cise.getSignAbnormalState() != null) {
				ci.setSignAbnormalState(cise.getSignAbnormalState().ordinal());
			}
		}
		ContainerInventoryRelieveEntity cire = containerInventoryRelieveService.inquireContainerInventoryRelieve(cie.getInventoryId());
		BeanUtils.copyProperties(cire, ci);
		
		if (cie.getRecieveNoticeId() != null && cie.getRecieveNoticeId() != 0L) {
			RecieveNoticeEntity rne = recieveNoticeService.inquireRecieveNoticeDetail(cie.getRecieveNoticeId());
			ci.setConfirmAccount(rne.getConfirmAccount());
			ci.setConfirmDateTime(rne.getConfirmDateTime());
			ci.setLogisticsCompanyName(rne.getLogisticsCompanyName());
		}

		List<ContainerBulkstockEntity> containerBulkstockList = containerBulkstockService.inquireContainerBulkstockList(cie.getInventoryId());
		for (ContainerBulkstockEntity cbe : containerBulkstockList) {
			ContainerBulkstock containerBulkstock = new ContainerBulkstock();
			BeanUtils.copyProperties(cbe, containerBulkstock);
			containerBulkstock.setMeasurement(cbe.getMeasurement().ordinal());
			containerBulkstock.setAbnormal(BooleanUtils.toIntegerObject(cbe.getAbnormal()));
			if (cbe.getQuantity() == null) {
				containerBulkstock.setQuantity(BigDecimal.ZERO);
			}
			if (containerBulkstock.getValuationQuantity() == null) {
				containerBulkstock.setValuationQuantity(BigDecimal.ZERO);
			}
			FinanceBulkstockEntity fbe = financeBulkstockService.inquireFinanceBulkstock(cbe.getBulkstockId());
			if (fbe == null) {
				continue;
			}
			containerBulkstock.setBulkstockPrice(fbe.getBulkstockPrice());
			containerBulkstock.setBulkstockValuation(containerBulkstock.getValuationQuantity().multiply(containerBulkstock.getBulkstockPrice()));
			containerBulkstock.setEvaluateValuation(containerBulkstock.getValuationQuantity().multiply(containerBulkstock.getEvaluatePrice()));
			containerBulkstock.setFinanceValuation(containerBulkstock.getValuationQuantity().multiply(containerBulkstock.getFinancePrice()));

			BulkstockInfoEntity bulkstockInfo = bulkstockInfoService.inquireBulkstockInfo(fbe.getBulkstockInfoId());
			containerBulkstock.setConversionFactor(bulkstockInfo.getConversionFactor());

			ci.getContainerBulkstocks().add(containerBulkstock);
		}
		return ci;
	}

	public static void fillBulkstockInventories(List<ContainerBulkstockEntity> containerBulkstockList, List<BulkstockInventory> bulkstockInventoryList) {
		Map<Long, BulkstockInventory> bulkstockInventories = new HashMap<>();
		for (ContainerBulkstockEntity cbe : containerBulkstockList) {
			BulkstockInventory bulkstockInventory = bulkstockInventories.get(cbe.getBulkstockId());
			if (bulkstockInventory == null) {
				bulkstockInventory = new BulkstockInventory();
				bulkstockInventory.setBulkstockInfoId(cbe.getBulkstockInfoId());
				bulkstockInventory.setBulkstockName(cbe.getBulkstockName());
				bulkstockInventory.setSpecification(cbe.getSpecification());
				bulkstockInventory.setQuantity(BigDecimal.ZERO);
				bulkstockInventory.setUnit(cbe.getUnit());
			}
			bulkstockInventory.setQuantity(bulkstockInventory.getQuantity().add(cbe.getQuantity()));
			bulkstockInventories.put(cbe.getBulkstockInfoId(), bulkstockInventory);
		}
		bulkstockInventoryList.addAll(bulkstockInventories.values());
	}

	// ===================================================================提货货物信息&提货货箱信息================================================================================================//
	public static void completeToPickupBulkstockList(List<PickupBulkstockEntity> bulkstockEntities, List<PickupBulkstock> pickupBulkstocks) {
		for (PickupBulkstockEntity pce : bulkstockEntities) {
			PickupBulkstock pc = new PickupBulkstock();
			BeanUtils.copyProperties(pce, pc);
			pc.setMeasurement(pce.getMeasurement().ordinal());
			if (pce.getBulkstockPrice() != null && pc.getValuationQuantity() !=null) {
				pc.setBulkstockValuation(pce.getBulkstockPrice().multiply(pc.getValuationQuantity()));
			}
			pc.setEvaluateValuation(pce.getEvaluatePrice().multiply(pc.getValuationQuantity()));
			pc.setFinanceValuation(pce.getFinancePrice().multiply(pc.getValuationQuantity()));
			pc.setValuationDate(DateUtil.formatDate(pce.getValuationDate()));

			BulkstockInfoEntity bulkstockInfo = bulkstockInfoService.inquireBulkstockInfo(pce.getBulkstockInfoId());
			pc.setConversionFactor(bulkstockInfo.getConversionFactor());

			pickupBulkstocks.add(pc);
		}
	}

	// ===================================================================异常货箱信息================================================================================================//
	public static AbnormalInventoryDetail toAbnormalInventory(AbnormalInventoryEntity entity) {
		AbnormalInventoryDetail info = new AbnormalInventoryDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAbnormalResult(entity.getAbnormalResult().ordinal());
		ContainerInventoryEntity inventory = containerInventoryService.inquireContainerInventory(entity.getInventoryId());
		info.setContainerInventory(toContainerInventory(inventory, Boolean.FALSE));
		return info;
	}

	public static DetainedInventory toDetainedInventory(DetainedInventoryEntity die) {
		DetainedInventory detainedInventory = new DetainedInventory();
		ContainerInventoryEntity inventory = containerInventoryService.inquireContainerInventory(die.getInventoryId());
		detainedInventory.setInventory(toContainerInventory(inventory, Boolean.FALSE));
		return detainedInventory;
	}

	/** 计算箱的融资金额 */
	public static BigDecimal caculateFinanceAmount(ContainerInventoryEntity cie) {
		BigDecimal financeAmount = cie.getAdjustment() == BigDecimal.ZERO || cie.getAdjustment() == null ? BigDecimal.ZERO : cie.getAdjustment();
		List<ContainerBulkstockEntity> containerBulkstockList = containerBulkstockService.inquireContainerBulkstockList(cie.getInventoryId());
		for (ContainerBulkstockEntity cbe : containerBulkstockList) {
			BigDecimal valuationQuantity = cbe.getValuationQuantity();
			if (valuationQuantity == null) {
				continue;
			}
			financeAmount = financeAmount.add(cbe.getValuationFinanceAmount());
			System.out.println("wenwenlong" + financeAmount);
		}
		return financeAmount;
	}

	/** 返回理货箱信息 */
	public static ContainerInventoryHistory toContainerInventoryHistory(ContainerInventoryHistoryEntity cieh) {
		ContainerInventoryHistory cih = new ContainerInventoryHistory();
		BeanUtils.copyProperties(cieh, cih);
		cih.setAbnormal(BooleanUtils.toIntegerObject(cieh.getAbnormal()));
		cih.setAbnormalState(cieh.getAbnormalState().ordinal());
		cih.setAbnormalType(cieh.getAbnormalType().ordinal());
		if (cieh.getTallyExAbnormalState() == null) {
			cih.setTallyExAbnormalState(0);
		} else {
			cih.setTallyExAbnormalState(cieh.getTallyExAbnormalState().ordinal());
		}
		cih.setTallyingState(BooleanUtils.toIntegerObject(cieh.getTallyingState()));

		List<ContainerBulkstockHistoryEntity> containerBulkstockList = containerBulkstockHistoryService.inquireContainerHistoryBulkstockList(cieh.getInventoryHistoryId());
		for (ContainerBulkstockHistoryEntity cbe : containerBulkstockList) {
			ContainerBulkstockHistory containerBulkstock = new ContainerBulkstockHistory();
			BeanUtils.copyProperties(cbe, containerBulkstock);
			containerBulkstock.setRecieveNoticeDetailId(cbe.getNoticeInventoryId());
			containerBulkstock.setMeasurement(cbe.getMeasurement().ordinal());
			containerBulkstock.setAbnormal(BooleanUtils.toIntegerObject(cbe.getAbnormal()));
			if (cbe.getQuantity() == null) {
				containerBulkstock.setQuantity(BigDecimal.ZERO);
			}
			if (containerBulkstock.getValuationQuantity() == null) {
				containerBulkstock.setValuationQuantity(BigDecimal.ZERO);
			}
			if (cieh.getFinanceId() != null && cieh.getFinanceId() != 0) {
				FinanceBulkstockEntity fbe = financeBulkstockService.inquireFinanceBulkstock(cbe.getBulkstockId());
				containerBulkstock.setBulkstockPrice(fbe.getBulkstockPrice());
				containerBulkstock.setBulkstockValuation(containerBulkstock.getValuationQuantity().multiply(containerBulkstock.getBulkstockPrice()));
				containerBulkstock.setEvaluateValuation(containerBulkstock.getValuationQuantity().multiply(containerBulkstock.getEvaluatePrice()));
				containerBulkstock.setFinanceValuation(containerBulkstock.getValuationQuantity().multiply(containerBulkstock.getFinancePrice()));

				BulkstockInfoEntity bulkstockInfo = bulkstockInfoService.inquireBulkstockInfo(fbe.getBulkstockInfoId());
				containerBulkstock.setConversionFactor(bulkstockInfo.getConversionFactor());
			}

			cih.getContainerBulkstocks().add(containerBulkstock);
		}
		return cih;
	}

}
